package edu.fiu.cis.objects;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;

import com.jwetherell.augmented_reality.R;
import com.jwetherell.augmented_reality.activity.AugmentedReality;
import com.jwetherell.augmented_reality.ui.Marker;
import com.jwetherell.augmented_reality.ui.objects.PaintableBoxedText;
import com.jwetherell.augmented_reality.ui.objects.PaintablePosition;

public class BIMMarker extends Marker{

	private static final int INVALID = -1000;
	private static final int MULTIPLICATION_FACTOR = 10;
	private Resources resource;
	private String name;
	private double latitude;
	private double longitude;
	private ArrayList<BIMLine> lineList;
	public BIMMarker(Resources resource,ArrayList<BIMLine> lineList, String name, double latitude, double longitude,
			double altitude, int color) {
		super(name, latitude, longitude, altitude, color);
		this.lineList =lineList;
		this.resource = resource;
//		try {
//			this.loadXML();
//		} catch (XmlPullParserException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
////		}
//		this.physicalLocation.set(this.latitude, this.longitude, altitude);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected synchronized void drawText(Canvas canvas) {
		
	}
	protected synchronized void drawIcon(Canvas canvas) {
        if (canvas == null) throw new NullPointerException();

        if (gpsSymbol == null) gpsSymbol = new PaintableBIM(this.lineList, 200,200,
        		Color.rgb(255, 255, 255),getColor());

        getScreenPosition().get(locationArray);
        float x = locationArray[0];
        float y = locationArray[1];
        if (AugmentedReality.portrait) {
            x -= gpsSymbol.getWidth() / 2;
            y -= gpsSymbol.getHeight();
        } else {
            y -= gpsSymbol.getHeight() / 2;
        }
        float currentAngle = 0;
        if (AugmentedReality.portrait) currentAngle = -90;

        if (symbolContainer == null) symbolContainer = new PaintablePosition(gpsSymbol, x, y, currentAngle, 1);
        else symbolContainer.set(gpsSymbol, x, y, currentAngle, 1);
        symbolContainer.paint(canvas);
    }
	
	 public void loadXML() throws XmlPullParserException, IOException
		{
	    	XmlResourceParser xpp = this.resource.getXml(R.xml.bigger);
	    	xpp.next();
	    	int eventType = xpp.getEventType();
	    	String xmlName = xpp.getName();
	    	while (eventType != XmlPullParser.END_DOCUMENT)
	    	{
	    		
	    		if(eventType == XmlPullParser.START_TAG && xmlName.equals("Campus"))
	    		{
	    			while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Campus"))
	    			{
	    				xmlName = xpp.getName();
	    				if(eventType == XmlPullParser.START_TAG && xmlName.equals("Location"))
	    				{
	    					while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Location"))
	    					{
	    						
	    						if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("Name"))
	    						{
	    							xpp.next();
	    							this.name  = xpp.getText();
	    							Log.i("Rajin","Got name:"+name);
	    						}
	    						else if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("Latitude"))
	    						{
	    							xpp.next();
	    							this.latitude = Double.parseDouble( xpp.getText());
	    							
	    						}
	    						else if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("Longitude"))
	    						{
	    							xpp.next();
	    							this.longitude = Double.parseDouble( xpp.getText());
	    						}
	    						eventType = xpp.next();
	    						xmlName = xpp.getName();
	    					}
	    				}
	    				else if(eventType == XmlPullParser.START_TAG && xmlName.equals("Building"))
	    				{
	    					while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Building"))
	    					{
	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("BuildingStorey"))
	    						{
	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("BuildingStorey"))
	    	    					{
	    	    						
	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PlanarGeometry"))
	    	    						{
	    	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PlanarGeometry"))
	    	    	    					{
	    	    	    						
	    	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PolyLoop"))
	    	    	    						{

    	    	    	    						double prevX = INVALID;
    	    	    	    						double prevY = INVALID;
    	    	    	    						double prevZ = INVALID;
	    	    	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PolyLoop"))
	    	    	    	    					{
	    	    	    								double x = INVALID;
	    	    	    								double y = INVALID;
	    	    	    								double z = INVALID;
	    	    	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("CartesianPoint"))
	    	    	    	    						{
	    	    	    	    							xpp.next();// star tag
	    	    	    	    							xpp.next();// Value tag
	    	    	    	    							String val = xpp.getText();
	    	    	    	    							xpp.next();//endtag;
	    	    	    	    							z = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
	    	    	    	    							xpp.next();// star tag
	    	    	    	    							xpp.next();// Value tag
	    	    	    	    							val = xpp.getText();
	    	    	    	    							xpp.next();//endtag;
	    	    	    	    							x = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
	    	    	    	    							
	    	    	    	    							xpp.next();// star tag
	    	    	    	    							xpp.next();// Value tag
	    	    	    	    							y = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
	    	    	    	    							xpp.next();//endtag; 
	    	    	    	    							if(prevX != INVALID && prevY != INVALID && prevZ != INVALID)
	    	    	    	    							{
	    	    	    	    								BIMLine line = new BIMLine(prevX, prevY,prevZ, x, y,z);
	    	    	    	    								
	    	    	    	    								BIMLine line2 = new BIMLine(prevX, prevY+200,prevZ, x, y+200,z);
	    	    	    	    								BIMLine line3 = new BIMLine(prevX, prevY,prevZ, prevX, prevY+200,prevZ);
	    	    	    	    								BIMLine line4 = new BIMLine(x, y,z, x, y+200,z);
	    	    	    	    								
	    	    	    	    								this.lineList.add(line);
	    	    	    	    								this.lineList.add(line2);
	    	    	    	    								this.lineList.add(line3);
	    	    	    	    								this.lineList.add(line4);
	    	    	    	    							}
	    	    	    	    							prevX = x;
	    	    	    	    							prevY = y;
	    	    	    	    							prevZ = z;
	    	    	    	    									
	    	    	    	    						}
	    	    	    	    						
	    	    	    	    						eventType = xpp.next();
	    	    	    	    						xmlName = xpp.getName();
	    	    	    	    					}
	    	    	    						}
	    	    	    						
	    	    	    						eventType = xpp.next();
	    	    	    						xmlName = xpp.getName();
	    	    	    					}
	    	    						}
	    	    						
	    	    						eventType = xpp.next();
	    	    						xmlName = xpp.getName();
	    	    					}
	    						}
	    						
	    						eventType = xpp.next();
	    						xmlName = xpp.getName();
	    					}
	    				}
	    				/*
	    				else if(eventType == XmlPullParser.START_TAG && xmlName.equals("Surface"))
	    				{
	    					while(eventType != XmlPullParser.END_TAG || !xmlName.equals("Surface"))
	    					{
	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PlanarGeometry"))
	    						{
	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PlanarGeometry"))
	    	    					{
	    	    						
	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("PolyLoop"))
	    	    						{

    	    	    						double prevX = INVALID;
    	    	    						double prevY = INVALID;
    	    	    						double prevZ = INVALID;
	    	    							while(eventType != XmlPullParser.END_TAG || !xmlName.equals("PolyLoop"))
	    	    	    					{
	    	    								double x = INVALID;
	    	    								double y = INVALID;
	    	    								double z = INVALID;
	    	    	    						if(eventType == XmlPullParser.START_TAG && xmlName.equals("CartesianPoint"))
	    	    	    						{
	    	    	    							xpp.next();// star tag
	    	    	    							xpp.next();// Value tag
	    	    	    							String val = xpp.getText();
	    	    	    							xpp.next();//endtag;
	    	    	    							z = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
	    	    	    							xpp.next();// star tag
	    	    	    							xpp.next();// Value tag
	    	    	    							val = xpp.getText();
	    	    	    							xpp.next();//endtag;
	    	    	    							x = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
	    	    	    							
	    	    	    							xpp.next();// star tag
	    	    	    							xpp.next();// Value tag
	    	    	    							y = Double.parseDouble(val)* MULTIPLICATION_FACTOR;
	    	    	    							xpp.next();//endtag; 
	    	    	    							if(prevX != INVALID && prevY != INVALID && prevZ != INVALID)
	    	    	    							{
	    	    	    								BIMLine line = new BIMLine(prevX, prevY,prevZ, x, y,z);
	    	    	    								
	    	    	    								BIMLine line2 = new BIMLine(prevX, prevY+200,prevZ, x, y+200,z);
	    	    	    								BIMLine line3 = new BIMLine(prevX, prevY,prevZ, prevX, prevY+200,prevZ);
	    	    	    								BIMLine line4 = new BIMLine(x, y,z, x, y+200,z);
	    	    	    								
	    	    	    								this.lineList.add(line);
	    	    	    								this.lineList.add(line2);
	    	    	    								this.lineList.add(line3);
	    	    	    								this.lineList.add(line4);
	    	    	    							}
	    	    	    							prevX = x;
	    	    	    							prevY = y;
	    	    	    							prevZ = z;
	    	    	    									
	    	    	    						}
	    	    	    						
	    	    	    						eventType = xpp.next();
	    	    	    						xmlName = xpp.getName();
	    	    	    					}
	    	    						}
	    	    						
	    	    						eventType = xpp.next();
	    	    						xmlName = xpp.getName();
	    	    					}
	    						}
	    						
	    						eventType = xpp.next();
	    						xmlName = xpp.getName();
	    					
	    					}
	    				}
	    				//*/
	    				eventType = xpp.next();
	    				xmlName = xpp.getName();
	    			}
	    		}
	    		
	    		eventType = xpp.next();
	    		xmlName = xpp.getName();
	    	
	    	}
	    	
	    	
		}
}
